package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;

public class GamePlay implements ApplicationListener {
	SpriteBatch m_SpriteHandler;
	Board m_board;
	int originX, originY;
	int index;
	int count = 0;
	Sprite sp_result;

	@Override
	public void create() {
		m_SpriteHandler = new SpriteBatch();
		originX = 100;
		originY = 30;
		m_board = new Board(originX, originY);
		// ResourceManagement.GetInstance().GetResource(ReSourceID.BOUND).setPosition(m_board.m_cells.get(98).rect.x,
		// m_board.m_cells.get(98).rect.y);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RESET_BOARD)
				.setPosition(originX + 10 * 50, originY + 10 * 50);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BACKGROUND)
				.setPosition(originX, originY);
		// quan co do
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED00)
				.setPosition(m_board.m_cells.get(85).rect.x,
						m_board.m_cells.get(85).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED10)
				.setPosition(m_board.m_cells.get(90).rect.x,
						m_board.m_cells.get(90).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED20)
				.setPosition(m_board.m_cells.get(91).rect.x,
						m_board.m_cells.get(91).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED30)
				.setPosition(m_board.m_cells.get(92).rect.x,
						m_board.m_cells.get(92).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED40)
				.setPosition(m_board.m_cells.get(93).rect.x,
						m_board.m_cells.get(93).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED50)
				.setPosition(m_board.m_cells.get(94).rect.x,
						m_board.m_cells.get(94).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED60)
				.setPosition(m_board.m_cells.get(95).rect.x,
						m_board.m_cells.get(95).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED70)
				.setPosition(m_board.m_cells.get(96).rect.x,
						m_board.m_cells.get(96).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED80)
				.setPosition(m_board.m_cells.get(97).rect.x,
						m_board.m_cells.get(97).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.RED90)
				.setPosition(m_board.m_cells.get(98).rect.x,
						m_board.m_cells.get(98).rect.y);
		// quan co xanh
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE00)
				.setPosition(m_board.m_cells.get(13).rect.x,
						m_board.m_cells.get(13).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE10)
				.setPosition(m_board.m_cells.get(8).rect.x,
						m_board.m_cells.get(8).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE20)
				.setPosition(m_board.m_cells.get(7).rect.x,
						m_board.m_cells.get(7).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE30)
				.setPosition(m_board.m_cells.get(6).rect.x,
						m_board.m_cells.get(6).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE40)
				.setPosition(m_board.m_cells.get(5).rect.x,
						m_board.m_cells.get(5).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE50)
				.setPosition(m_board.m_cells.get(4).rect.x,
						m_board.m_cells.get(4).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE60)
				.setPosition(m_board.m_cells.get(3).rect.x,
						m_board.m_cells.get(3).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE70)
				.setPosition(m_board.m_cells.get(2).rect.x,
						m_board.m_cells.get(2).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE80)
				.setPosition(m_board.m_cells.get(1).rect.x,
						m_board.m_cells.get(1).rect.y);
		ResourceManagement
				.GetInstance()
				.GetResource(ReSourceID.BLUE90)
				.setPosition(m_board.m_cells.get(0).rect.x,
						m_board.m_cells.get(0).rect.y);

	}

	@Override
	public void dispose() {
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		// update
		/*
		Vector3 touchpos = new Vector3();
		if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
			count++;
			System.out.println(count);
			int index = m_board.GetCell(touchpos.x, Gdx.graphics.getHeight()
					- touchpos.y);
			if (count % 2 != 0) {
				sp_result = ResourceManagement.GetInstance().GetSprite(
						m_board.m_cells.get(index).id,
						m_board.m_cells.get(index).numberofpoint);
			} else {
				touchpos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
				sp_result.setPosition(m_board.m_cells.get(index).rect.x,
						m_board.m_cells.get(index).rect.y);
				sp_result = null;
			}
			*/
			/*
			 * if(m_board.m_cells.get(index).flag == false) {
			 * ResourceManagement.
			 * GetInstance().GetResource(ReSourceID.BOUND).setPosition
			 * (m_board.m_cells.get(index).rect.x,
			 * m_board.m_cells.get(index).rect.y); }
			 */
			// ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE90).setPosition(m_board.m_cells.get(index).rect.x,
			// m_board.m_cells.get(index).rect.y);
			// index = m_board.GetCell(touchpos);
			// ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE00).setPosition(m_board.m_cells.get(index).rect.x,
			// m_board.m_cells.get(index).rect.y);
			/*
			 * ResourceManagement.GetInstance().GetResource(ReSourceID.RESET_BOARD
			 * ).setPosition(touchpos.x -
			 * ResourceManagement.GetInstance().GetResource
			 * (ReSourceID.RESET_BOARD).getWidth()/2, Gdx.graphics.getHeight() -
			 * touchpos.y -
			 * ResourceManagement.GetInstance().GetResource(ReSourceID
			 * .RESET_BOARD).getHeight()/2 );
			 */
		//}
		// draw
		m_SpriteHandler.begin();

		ResourceManagement.GetInstance().GetResource(ReSourceID.RED00)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RESET_BOARD)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BACKGROUND)
				.draw(m_SpriteHandler);
		// ve quan co do
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED00)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED10)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED20)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED30)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED40)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED50)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED60)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED70)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED80)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.RED90)
				.draw(m_SpriteHandler);
		// ve quan co xanh
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE00)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE10)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE20)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE30)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE40)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE50)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE60)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE70)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE80)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BLUE90)
				.draw(m_SpriteHandler);
		ResourceManagement.GetInstance().GetResource(ReSourceID.BOUND)
				.draw(m_SpriteHandler);
		m_SpriteHandler.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
